package cfig.ota

import cfig.toolset.AdbTool
import org.slf4j.LoggerFactory
import se.vidstige.jadb.JadbDevice
import java.io.File

class UpdatePkgRecovery(file: String) {
    private val log = LoggerFactory.getLogger(UpdatePkgRecovery::class.java)
    private val fileName: String = file

    fun installTo(dut: JadbDevice) {
        val adt = AdbTool()
        adt.shellCheckCall(dut, "mkdir -p /cache/recovery")
        adt.checkCall(dut, "push $fileName /cache/recovery/update.zip")
        val recoveryCmd = """
--update_package=/cache/recovery/update.zip
--stages=3
--security""".trimMargin()
        log.info("[$recoveryCmd]")
        File("command").writeText(recoveryCmd)
        adt.checkCall(dut, "push command /cache/recovery/command")
        File("command").deleteOnExit()
        adt.shellCheckCall(dut, "cat /cache/recovery/command")
        adt.checkCall(dut, "reboot recovery")
    }
}
